home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / ObjLoad / ObjLoad.java.z / ObjLoad.java
Encoding:
Java Source  |  2003-08-08  |  8.5 KB  |  281 lines

  1. /*
  2.  *    @(#)ObjLoad.java 1.22 02/10/21 13:46:12
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import com.sun.j3d.loaders.objectfile.ObjectFile;
  41. import com.sun.j3d.loaders.ParsingErrorException;
  42. import com.sun.j3d.loaders.IncorrectFormatException;
  43. import com.sun.j3d.loaders.Scene;
  44. import java.applet.Applet;
  45. import java.awt.*;
  46. import java.awt.event.*;
  47. import com.sun.j3d.utils.applet.MainFrame;
  48. import com.sun.j3d.utils.universe.*;
  49. import javax.media.j3d.*;
  50. import javax.vecmath.*;
  51. import java.io.*;
  52. import com.sun.j3d.utils.behaviors.vp.*;
  53. import java.net.URL;
  54. import java.net.MalformedURLException;
  55.  
  56. public class ObjLoad extends Applet {
  57.  
  58.     private boolean spin = false;
  59.     private boolean noTriangulate = false;
  60.     private boolean noStripify = false;
  61.     private double creaseAngle = 60.0;
  62.     private URL filename = null;
  63.     private SimpleUniverse u;
  64.     private BoundingSphere bounds;
  65.  
  66.     public BranchGroup createSceneGraph() {
  67.     // Create the root of the branch graph
  68.     BranchGroup objRoot = new BranchGroup();
  69.  
  70.         // Create a Transformgroup to scale all objects so they
  71.         // appear in the scene.
  72.         TransformGroup objScale = new TransformGroup();
  73.         Transform3D t3d = new Transform3D();
  74.         t3d.setScale(0.7);
  75.         objScale.setTransform(t3d);
  76.         objRoot.addChild(objScale);
  77.  
  78.     // Create the transform group node and initialize it to the
  79.     // identity.  Enable the TRANSFORM_WRITE capability so that
  80.     // our behavior code can modify it at runtime.  Add it to the
  81.     // root of the subgraph.
  82.     TransformGroup objTrans = new TransformGroup();
  83.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  84.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  85.     objScale.addChild(objTrans);
  86.  
  87.     int flags = ObjectFile.RESIZE;
  88.     if (!noTriangulate) flags |= ObjectFile.TRIANGULATE;
  89.     if (!noStripify) flags |= ObjectFile.STRIPIFY;
  90.     ObjectFile f = new ObjectFile(flags, 
  91.       (float)(creaseAngle * Math.PI / 180.0));
  92.     Scene s = null;
  93.     try {
  94.       s = f.load(filename);
  95.     }
  96.     catch (FileNotFoundException e) {
  97.       System.err.println(e);
  98.       System.exit(1);
  99.     }
  100.     catch (ParsingErrorException e) {
  101.       System.err.println(e);
  102.       System.exit(1);
  103.     }
  104.     catch (IncorrectFormatException e) {
  105.       System.err.println(e);
  106.       System.exit(1);
  107.     }
  108.       
  109.     objTrans.addChild(s.getSceneGroup());
  110.  
  111.     bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  112.  
  113.         if (spin) {
  114.       Transform3D yAxis = new Transform3D();
  115.       Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
  116.                       0, 0,
  117.                       4000, 0, 0,
  118.                       0, 0, 0);
  119.  
  120.       RotationInterpolator rotator =
  121.           new RotationInterpolator(rotationAlpha, objTrans, yAxis,
  122.                        0.0f, (float) Math.PI*2.0f);
  123.       rotator.setSchedulingBounds(bounds);
  124.       objTrans.addChild(rotator);
  125.     } 
  126.  
  127.         // Set up the background
  128.         Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
  129.         Background bgNode = new Background(bgColor);
  130.         bgNode.setApplicationBounds(bounds);
  131.         objRoot.addChild(bgNode);
  132.  
  133.     return objRoot;
  134.     }
  135.  
  136.     private void usage()
  137.     {
  138.       System.out.println(
  139.     "Usage: java ObjLoad [-s] [-n] [-t] [-c degrees] <.obj file>");
  140.       System.out.println("  -s Spin (no user interaction)");
  141.       System.out.println("  -n No triangulation");
  142.       System.out.println("  -t No stripification");
  143.       System.out.println(
  144.     "  -c Set crease angle for normal generation (default is 60 without");
  145.       System.out.println(
  146.     "     smoothing group info, otherwise 180 within smoothing groups)");
  147.       System.exit(0);
  148.     } // End of usage
  149.  
  150.  
  151.  
  152.     public void init() {
  153.     if (filename == null) {
  154.             // Applet
  155.             try {
  156.                 URL path = getCodeBase();
  157.                 filename = new URL(path.toString() + "./galleon.obj");
  158.             }
  159.             catch (MalformedURLException e) {
  160.           System.err.println(e);
  161.           System.exit(1);
  162.             }
  163.     }
  164.  
  165.     setLayout(new BorderLayout());
  166.         GraphicsConfiguration config =
  167.            SimpleUniverse.getPreferredConfiguration();
  168.  
  169.         Canvas3D c = new Canvas3D(config);
  170.     add("Center", c);
  171.  
  172.     // Create a simple scene and attach it to the virtual universe
  173.     BranchGroup scene = createSceneGraph();
  174.     u = new SimpleUniverse(c);
  175.     
  176.     // add mouse behaviors to the ViewingPlatform
  177.     ViewingPlatform viewingPlatform = u.getViewingPlatform();
  178.  
  179.     PlatformGeometry pg = new PlatformGeometry();
  180.  
  181.     // Set up the ambient light
  182.     Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
  183.     AmbientLight ambientLightNode = new AmbientLight(ambientColor);
  184.     ambientLightNode.setInfluencingBounds(bounds);
  185.     pg.addChild(ambientLightNode);
  186.  
  187.     // Set up the directional lights
  188.     Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
  189.     Vector3f light1Direction  = new Vector3f(1.0f, 1.0f, 1.0f);
  190.     Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  191.     Vector3f light2Direction  = new Vector3f(-1.0f, -1.0f, -1.0f);
  192.  
  193.     DirectionalLight light1
  194.         = new DirectionalLight(light1Color, light1Direction);
  195.     light1.setInfluencingBounds(bounds);
  196.     pg.addChild(light1);
  197.  
  198.     DirectionalLight light2
  199.         = new DirectionalLight(light2Color, light2Direction);
  200.     light2.setInfluencingBounds(bounds);
  201.     pg.addChild(light2);
  202.  
  203.     viewingPlatform.setPlatformGeometry( pg );
  204.       
  205.     // This will move the ViewPlatform back a bit so the
  206.     // objects in the scene can be viewed.
  207.     viewingPlatform.setNominalViewingTransform();
  208.  
  209.     if (!spin) {
  210.             OrbitBehavior orbit = new OrbitBehavior(c,
  211.                             OrbitBehavior.REVERSE_ALL);
  212.             BoundingSphere bounds =
  213.                 new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  214.             orbit.setSchedulingBounds(bounds);
  215.             viewingPlatform.setViewPlatformBehavior(orbit);        
  216.     }
  217.  
  218.     u.addBranchGraph(scene);
  219.     }
  220.  
  221.     // Caled if running as a program
  222.     public ObjLoad(String[] args) {
  223.       if (args.length != 0) {
  224.     for (int i = 0 ; i < args.length ; i++) {
  225.       if (args[i].startsWith("-")) {
  226.         if (args[i].equals("-s")) {
  227.           spin = true;
  228.         } else if (args[i].equals("-n")) {
  229.           noTriangulate = true;
  230.         } else if (args[i].equals("-t")) {
  231.           noStripify = true;
  232.         } else if (args[i].equals("-c")) {
  233.           if (i < args.length - 1) {
  234.         creaseAngle = (new Double(args[++i])).doubleValue();
  235.           } else usage();
  236.         } else {
  237.           usage();
  238.         }
  239.       } else {
  240.         try {
  241.           if ((args[i].indexOf("file:") == 0) ||
  242.           (args[i].indexOf("http") == 0)) {
  243.           filename = new URL(args[i]);
  244.           }
  245.           else if (args[i].charAt(0) != '/') {
  246.           filename = new URL("file:./" + args[i]);
  247.           }
  248.           else {
  249.           filename = new URL("file:" + args[i]);
  250.           }
  251.             }
  252.         catch (MalformedURLException e) {
  253.           System.err.println(e);
  254.           System.exit(1);
  255.         }
  256.       }
  257.     }
  258.       }
  259.     }
  260.  
  261.  
  262.  
  263.     // Running as an applet
  264.     public ObjLoad() {
  265.     }
  266.  
  267.     public void destroy() {
  268.     u.cleanup();
  269.     }
  270.  
  271.  
  272.  
  273.     //
  274.     // The following allows ObjLoad to be run as an application
  275.     // as well as an applet
  276.     //
  277.     public static void main(String[] args) {
  278.       new MainFrame(new ObjLoad(args), 700, 700);
  279.     }
  280. }
  281.